<?php

class AdminAction extends Action{

    function login() {
    	$this->display();
    }


    function checklogin(){
		$username = $this->_isempty($_POST['username'],'账号不能为空');
		$password = $this->_isempty($_POST['password'],'密码不能为空');
		if(md5($_POST['verify']) != $_SESSION['verify']){
			$this->assign('jumpUrl',__APP__.'/Admin/login/');
			$this->assign('waitSecond','3');
			$this->error("验证码错误，请重新输入");
		}
		$m = M('user');
		$data['username'] = $username;
		$data['password_des'] = encrypt($password);

		$list = $m->where("password_des= '".encrypt($password)."' and (username ='".$username."' or SJHM ='".$username."')")->find();
		$username=$list['username'];
		//echo $m->getlastsql();
		//exit;

		if($list != null){
			$_SESSION['scode'] = C('SYSTEM_CODE');
			$_SESSION['susername'] = $username;
			$_SESSION['spassword'] = $password;
			$_SESSION['suserid'] = $list['id'];


			if($list['id'] != null){
				$t = M('teacher');
				$tt['user_id'] = $list['id'];
				$tlist = $t->where($tt)->find();
				// if($tlist == null){
				// 	$this->error('无此角色');
				// }
				$_SESSION['teacher_id'] = $tlist['id'];
				$_SESSION['schooler_id'] = $tlist['id'];
				$_SESSION['school_id'] = $tlist['school_id'];

				$logid = add_log($tlist['id'],'登录 ID:'.$tlist['id'].'-'.$username,1);
				$_SESSION['logid'] = $logid;
			}



			if ($list['is_parent']==0)
			{
				if($list['is_teacher'] == 1 && $list['is_school'] ==1 ){

					$this->assign('userinfo',$list);
					$this->display('selectuser');
				}else if($list['is_teacher'] == 1 && $list['is_school'] == 0){
					$_SESSION['type'] = 'teacher';				
					$this->redirect('Teacher/index');
				}else if($list['is_teacher'] == 0 && $list['is_school'] == 1){
					$_SESSION['type'] = 'school';
					$this->redirect('Teacher/index');
				}else if($list['is_teacher'] == 0 && $list['is_school'] == 0){
					$this->assign('jumpUrl',__APP__.'/Admin/login/');
					$this->assign('waitSecond','3');
					$this->error("权限不足，请联系管理员");
				}
			}
			else
			{
				if($list['is_teacher'] == 0 && $list['is_school'] ==0){
					$_SESSION['type'] = 'parent';
					$parentinfo = loadinfo($_SESSION['suserid'],2);
					$_SESSION['parent_id']=$parentinfo['id'];
					$_SESSION['user_id']=getinfo($_SESSION['suserid'],'user','id');
					$this->redirect('Parent/index');
				}else {

					$this->assign('userinfo',$list);
					$this->display('selectuser');
				}
			}
		}else{
			$this->assign('jumpUrl',__APP__.'/Admin/login/');
			$this->assign('waitSecond','3');
			$this->error("密码错误，请重新输入");
		}
    }

    public function selectuser()
    {
    	$map['id'] =$_SESSION['suserid'];
    	$muser =M('user');
    	$userinfo = $muser-> where($map)->find();
    	print_r($userinfo);
    	$this->assign('userinfo',$userinfo);
    	$this->display();
    }
    public function selectrole(){
		$username = $this->_isempty($_SESSION['susername'],'账号不能为空');
		$password = $this->_isempty($_SESSION['spassword'],'密码不能为空');

		$type = $this->_isempty($_GET['type'],'选择登录类型');

		$m = M('user');
		$data['username'] = $username;
		$data['password_des'] = encrypt($password);
		$list = $m->where($data)->find();

		if($list != null){
			if($type == 'teacher'){
				$_SESSION['type'] = 'teacher';
//				$this->assign('jumpUrl',__APP__.'/Teacher/index/');
//				$this->assign('waitSecond','3');
//				$this->success("登录成功");
				$this->redirect('Teacher/index');
			}else if($type == 'school'){
				$_SESSION['type'] = 'school';
//				$this->assign('jumpUrl',__APP__.'/School/index/');
//				$this->assign('waitSecond','3');
//				$this->success("登录成功");
				$this->redirect('School/index');

			}else if($type == 'parent'){
				$_SESSION['type'] = 'parent';
//				$this->assign('jumpUrl',__APP__.'/School/index/');
//				$this->assign('waitSecond','3');
//				$this->success("登录成功");
				$parentinfo = loadinfo($_SESSION['suserid'],2);
				$_SESSION['parent_id']=$parentinfo['id'];
				$_SESSION['user_id']=getinfo($_SESSION['suserid'],'user','id');
					
				$this->redirect('Parent/index');
			}else{
				$this->error('非法操作');
				exit;
			}
		}else{
				$this->assign('jumpUrl',__APP__.'/Admin/login/');
				$this->assign('waitSecond','3');
				$this->error("密码错误，请重新输入");
		}
    }


	public function _isempty($key,$str){
		if(empty($key)){
			$this->error($str);
		}else{
			return $key;
		}
	}

	public function logout(){
		outlog($_SESSION['logid'],'登出后台',1);
		session_destroy();
		$this->assign('jumpUrl',__APP__.'/Admin/login/');
		$this->assign('waitSecond','3');
		$this->success("注销成功！");
	}



	public function verify()
	{
		import('@.ORG.Image');
		Image::buildImageVerify(5,1,gif,78,30,'verify');
	}

}
?>